package com.woniuxy.exam.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.woniuxy.commons.entity.*;
import com.woniuxy.exam.listener.*;
import com.woniuxy.exam.service.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author zouyang
 * @date 2020/7/21 - 16:35
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private BlankFillingService blankFillingService;
    @Autowired
    private MultipleChoiceService multipleChoiceService;
    @Autowired
    private ShortAnswerService shortAnswerService;
    @Autowired
    private SingleChoiceService singleChoiceService;
    @Autowired
    private TrueOrFalseService trueOrFalseService;

    @PostMapping(value = "/excel")
    public ResponseEntity importFromExcel(MultipartFile uploadExcel){
        if(uploadExcel==null&&uploadExcel.isEmpty()){
            return new ResponseEntity(403,"没有接收到上传的文件");
        }
        //获取文件的原始名称
        String fileName = uploadExcel.getOriginalFilename();
        //获取文件后缀
        String extension = FilenameUtils.getExtension(fileName);
        //创建流对象
        InputStream in;
        ExcelReader excelReader = null;
        if(extension.equals("xlsx")||extension.equals("xls")) {
            try {
                /**
                 * 参数一：in 为输入流
                 * 参数二：BlankFilling.class 为要封装的类
                 * 参数三：XxxListener为自定一的监听器
                 */
                in = uploadExcel.getInputStream();
                excelReader = EasyExcel.read(in).build();
                ReadSheet readSheet = EasyExcel.readSheet(1).head(SingleChoice.class)
                        .registerReadListener(new SCListener(singleChoiceService)).build();
                excelReader.read(readSheet);

                //因为每次read都要关闭流所以如果想要继续读取其他表，就需要再次重建输入流
                in = uploadExcel.getInputStream();
                excelReader = EasyExcel.read(in).build();
                readSheet = EasyExcel.readSheet(2).head(MultipleChoice.class)
                        .registerReadListener(new MCListener(multipleChoiceService)).build();
                excelReader.read(readSheet);

                in = uploadExcel.getInputStream();
                excelReader = EasyExcel.read(in).build();
                readSheet = EasyExcel.readSheet(3).head(TrueOrFalse.class)
                        .registerReadListener(new TOFListener(trueOrFalseService)).build();
                excelReader.read(readSheet);

                in = uploadExcel.getInputStream();
                excelReader = EasyExcel.read(in).build();
                readSheet = EasyExcel.readSheet(4).head(BlankFilling.class)
                        .registerReadListener(new BFListener(blankFillingService)).build();
                excelReader.read(readSheet);

                in = uploadExcel.getInputStream();
                excelReader = EasyExcel.read(in).build();
                readSheet = EasyExcel.readSheet(5).head(ShortAnswer.class)
                        .registerReadListener(new SAListener(shortAnswerService)).build();
                excelReader.read(readSheet);

            } catch (IOException e) {
                e.printStackTrace();
                return new ResponseEntity(500,"系统异常");
            }finally {
                if(excelReader!=null){
                    excelReader.finish();
                }
            }
            return new ResponseEntity(200, "success");
        }
        return new ResponseEntity(402,"文件格式不对");
    }
}
